package com.starlink.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.hutool.core.convert.Convert;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.PageHelper;
import com.starlink.common.enums.ErrorCode;
import com.starlink.common.exception.BusinessException;
import org.springframework.data.domain.Pageable;
import com.starlink.common.utils.AssertUtil;
import com.starlink.model.dtomapper.StarNurturingDtoMapper;
import com.starlink.model.domain.StarNurturingDO;
import com.starlink.mapper.StarNurturingMapper;
import com.starlink.model.request.StarNurturingRequest;
import com.starlink.service.StarNurturingService;
import com.starlink.model.util.StarNurturingConvert;
import com.starlink.model.vo.StarNurturingVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import static com.starlink.utils.PageUtils.getOrderBy;

import java.util.List;

/**
 * @author zhanghong
 */
@Service
public class StarNurturingServiceImpl extends ServiceImpl<StarNurturingMapper, StarNurturingDO> implements StarNurturingService {
    @Autowired
    private StarNurturingMapper starNurturingMapper;

    @Override
    public StarNurturingVO getByKey(long id) {
        StarNurturingVO starNurturingVO = StarNurturingDtoMapper.INSTANCE.do2VO(getById(id));
        return starNurturingVO;
    }

    @Override
    public PageInfo<StarNurturingVO> pageQuery(Pageable pageable,StarNurturingRequest request) {
        PageHelper.startPage(request.getPageNum(), request.getPageSize(), getOrderBy(pageable.getSort())).setReasonable(true);
        List<StarNurturingDO> list = starNurturingMapper.listQuery(request);
        List<StarNurturingVO> starNurturingVOList = StarNurturingConvert.buildStarNurturingVoList(list);
        return new PageInfo(starNurturingVOList);
    }

    @Override
    public List<StarNurturingVO> listQuery(StarNurturingRequest request) {
        List<StarNurturingDO> list = starNurturingMapper.selectDOList(StarNurturingDtoMapper.INSTANCE.request2DO(request));
        List<StarNurturingVO> starNurturingVOS = StarNurturingConvert.buildStarNurturingVoList(list);
        return starNurturingVOS;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean add(StarNurturingRequest request) {
        try {
            save(StarNurturingDtoMapper.INSTANCE.request2DO(request));
        } catch (Exception ex) {
            if (ex.getMessage().contains("Data too long")) {
                throw new BusinessException(ErrorCode.PARAM_ERROR, "详情内容超长，请优化！");
            }
        }
        return true;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean updateByKey(StarNurturingRequest request) {
        AssertUtil.assertNotNull(request.getId(), "id不能为空");
        try {
            saveOrUpdate(StarNurturingDtoMapper.INSTANCE.request2DO(request));
        } catch (Exception ex) {
            if (ex.getMessage().contains("Data too long")) {
                throw new BusinessException(ErrorCode.PARAM_ERROR, "详情内容超长，请优化！");
            }
        }
        return true;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean deleteDOByIds(String ids) {
        starNurturingMapper.deleteDOByIds(Convert.toStrArray(ids));
        return true;
    }
}
